import Vue from 'vue'
import Router from 'vue-router'
import VueCookies from 'vue-cookies'
Vue.use(Router)

const home = () => import('@/view/home.vue')
const bloodWarehousing = () => import('@/view/bloodWarehousing.vue')
const bloodApplicationForm = () => import('@/view/bloodApplicationForm.vue')
const orgBloodletting = () => import('@/view/orgBloodletting.vue')
const bloodReturnScrap = () => import('@/view/bloodReturnScrap.vue')
const bloodRecoveryRegister = () => import('@/view/bloodRecoveryRegister.vue')
const labelSigning = () => import('@/view/labelSigning.vue')
const messageManagement = () => import('@/view/messageManagement.vue')
const orgBloodStatistics = () => import('@/view/orgBloodStatistics.vue')
const personManage = () => import('@/view/personManage.vue')
const systemManage = () => import('@/view/systemManage.vue')
const rootManage = () => import('@/view/rootManage.vue')
const orgManage = () => import('@/view/orgManage.vue')
const bloodApplicationFormExamine = () => import('@/view/bloodApplicationFormExamine.vue')
const bloodStatistics = () => import('@/view/bloodStatistics.vue')
const bloodWarehouseLossReport = () => import('@/view/bloodWarehouseLossReport.vue')
const login = () => import('@/view/login.vue')
const index = () => import('@/view/index.vue')
const useBloodCaseStatistics = () => import('@/view/useBloodCaseStatistics.vue')
const roleManage = () => import('@/view/roleManage.vue')
const bloodTransfusionStatistics = () => import('@/view/bloodTransfusionStatistics.vue')
const bloodAuthorizationLetter = () => import('@/view/bloodAuthorizationLetter.vue')
const bloodCrossMatching = () => import('@/view/bloodCrossMatching.vue')

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      redirect: '/',
      component: index,
      children: [
        //首页
        {
          path: '/',
          name: 'home',
          component: home
        },
        //血制品入库
        {
          path: '/bloodWarehousing',
          name: 'bloodWarehousing',
          component: bloodWarehousing
        },
        //用血申请单
        {
          path: '/bloodApplicationForm',
          name: 'bloodApplicationForm',
          component: bloodApplicationForm
        },
        //科室发血
        {
          path: '/orgBloodletting',
          name: 'orgBloodletting',
          component: orgBloodletting
        },
        //血制品返回血站与报废
        {
          path: '/bloodReturnScrap',
          name: 'bloodReturnScrap',
          component: bloodReturnScrap
        },
        //血袋回收登记
        {
          path: '/bloodRecoveryRegister',
          name: 'bloodRecoveryRegister',
          component: bloodRecoveryRegister
        },
        //标签签收
        {
          path: '/labelSigning',
          name: 'labelSigning',
          component: labelSigning
        },
        //消息管理
        {
          path: '/messageManagement',
          name: 'messageManagement',
          component: messageManagement
        },
        //科室用血情况明细统计
        {
          path: '/orgBloodStatistics',
          name: 'orgBloodStatistics',
          component: orgBloodStatistics
        },
        //人员管理与授权
        {
          path: '/personManage',
          name: 'personManage',
          component: personManage
        },
        //菜单资源管理
        {
          path: '/systemManage',
          name: 'systemManage',
          component: systemManage
        },
        //权限资源管理
        {
          path: '/rootManage',
          name: 'rootManage',
          component: rootManage
        },
        //权限资源管理
        {
          path: '/orgManage',
          name: 'orgManage',
          component: orgManage
        },
        //用血申请单审核
        {
          path: '/bloodApplicationFormExamine',
          name: 'bloodApplicationFormExamine',
          component: bloodApplicationFormExamine
        },
        //输血量统计
        {
          path: '/bloodStatistics',
          name: 'bloodStatistics',
          component: bloodStatistics
        },
        //血液库房报损单
        {
          path: '/bloodWarehouseLossReport',
          name: 'bloodWarehouseLossReport',
          component: bloodWarehouseLossReport
        },
        //科室用血情况明细统计 
        {
          path: 'useBloodCaseStatistics',
          name: 'useBloodCaseStatistics',
          component: useBloodCaseStatistics
        },
        //角色管理与授权 
        {
          path: 'roleManage',
          name: 'roleManage',
          component: roleManage
        },
        //输血前检测比例统计
        {
          path: '/bloodTransfusionStatistics',
          name: 'bloodTransfusionStatistics',
          component: bloodTransfusionStatistics
        },
        //输血前检测比例统计
        {
          path: '/bloodAuthorizationLetter',
          name: 'bloodAuthorizationLetter',
          component: bloodAuthorizationLetter
        },
        //交叉配血
        {
          path: '/bloodCrossMatching',
          name: 'bloodCrossMatching',
          component: bloodCrossMatching
        },
      ]
    },
    //登陆
    {
      path: '/login',
      name: 'login',
      component: login
    },

  ]
})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  let isKey = router.app.$cookies.isKey('userInfo')
  if (!isKey) {
    console.log(to.path);
    if (to.path != "/login") {
      next({
        path: '/login'
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
